<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar title="登录" class="page-nav-bar">
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>
    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field name="mobile" placeholder="请输入手机号" v-model="user.mobile" :rules="loginFormRules.mobile" type="number" maxlength="11">
        <i slot="left-icon" class="iconfont iconshouji"></i>
      </van-field>
      <van-field name="code" placeholder="请输入验证码" v-model="user.code" :rules="loginFormRules.code" type="number" maxlength="6">
        <i slot="left-icon" class="iconfont iconyanzhengma"></i>
        <template #button>
          <!-- 倒计时 -->
          <van-count-down :time="1000 * 60" format="ss s" v-if="showCount" @finish="showCount = false"/>
          <van-button size="small" round type="default" class="verification-btn" v-else @click="onSendCode" native-type="button">发送验证码</van-button>
        </template>
      </van-field>
      <div class="login-wrap-btn">
        <van-button block type="info" native-type="submit" class="login-btn">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
// 导入请求接口
import { login, sendSms } from '@/api/user'
export default {
  name: 'loginPage',
  data() {
    return {
      // 用户手机号和验证码
      user: {
        mobile: '13911111111',
        // code: '246810'
        code: ''
      },
      // 表单验证
      loginFormRules: {
        mobile: [
          { required: true, message: '手机号不能为空' },
          { pattern: /^1[3|5|7|8]\d{9}$/, message: '手机号格式不正确' }
        ],
        code: [
          { required: true, message: '验证码不能为空' },
          { pattern: /^\d{6}$/, message: '验证码格式不正确' }
        ]
      },
      // 倒计时的显示和隐藏
      showCount: false
    }
  },
  created() {},
  mounted() {},
  methods: {
    // 点击提交表单
    async onSubmit() {
      // 加载提示
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        // 设置转动时间，为0就无限转动
        duration: 0
      })
      // 发送请求
      try {
        const { data } = await login(this.user)
        this.$store.commit('getUserList', data.data)
        this.$toast.success('登录成功')
        // 展示后退
        this.$router.go(-1)
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('手机号或验证码错误')
        } else {
          this.$toast.fail('登录失败，等稍后再试')
        }
      }
    },
    // 点击获取验证码
    async onSendCode() {
      try {
        // 根据name属性来验证
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return this.$toast.fail('请输入正确的手机号')
      }
      // 手机号验证成功显示倒计时
      this.showCount = true
      // 获取验证码
      try {
        await sendSms(this.user.mobile)
      } catch (err) {
        // 发送失败关闭倒计时
        this.showCount = false
        if (err.response.status === 429) {
          this.$toast.fail('获取验证码太频繁，请稍后再试')
        } else {
          this.$toast.fail('获取验证码失败，等稍后再试')
        }
      }
    }
  }
}
</script>

<style scoped lang="less">
.iconfont {
  font-size: 37px;
}
.verification-btn {
  width: 152px;
  height: 46px;
  background-color: #ededed;
  font-size: 22px;
  color: #666;
  line-height: 46px;
}
.login-wrap-btn {
  padding: 52px 30px 0;
  .login-btn {
    background-color: #6db4fb;
    border: none;
    color: #fff;
  }
}
// .page-nav-bar {
//   background-color: #3296fa;
//   /deep/.van-nav-bar__title {
//     color: #fff;
//   }
// }
</style>
